package com.dingdang.controller.account_management;


import com.dingdang.data.Result;
import com.dingdang.em.ResultEnum;
import com.dingdang.entity.Admin;
import com.dingdang.mapper.AdminMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/adminController")
@Api("管理员管理接口")
public class AdminController
{
    @Autowired
    private AdminMapper adminMapper;

    //搜索
    @GetMapping
    @ApiOperation("管理员搜索")
    @ApiImplicitParam(name = "void",value = "空",required = true,paramType = "query")
    public Result selectAdmin()
    {
        Admin admin = new Admin();
        List<Admin> adminList= adminMapper.selectList(null);
        return Result.response(ResultEnum.SUCCESS).setData(adminList);
    }

    //管理员增加
    @PostMapping
    @ApiOperation("管理员增加")
    @ApiImplicitParam(name = "User",value = "管理员实体类",required = true,paramType = "body")
    public Result insertAdmin(@RequestBody Admin admin)
    {
        int i = adminMapper.insert(admin);
        if (i==1)
        {
            return Result.response(ResultEnum.SUCCESS);
        }
        else
        {
            return Result.response(ResultEnum.ERROR);
        }
    }

    //管理员回显
    @PostMapping("/adminToUpdate")
    @ApiOperation("管理员回显")
    @ApiImplicitParam(name = "id",value = "管理员id",required = true,paramType = "query")
    public Result ToUpdateAdmin(@RequestParam("id") Long id)
    {
        Admin admin = new Admin();
        admin = (Admin)adminMapper.selectById(id);
        return Result.response(ResultEnum.SUCCESS).setData(admin);
    }

    //管理员修改
    @PutMapping
    @ApiOperation("管理员修改")
    @ApiImplicitParam(name = "Admin",value = "管理员实体类",required = true,paramType = "body")
    public Result UpdateAdmin(@RequestBody Admin admin)
    {
        int i = adminMapper.updateById(admin);
        if (i==1)
        {
            return Result.response(ResultEnum.SUCCESS);
        }
        else
        {
            return Result.response(ResultEnum.ERROR);
        }
    }

    @DeleteMapping()
    @ApiOperation("管理员删除")
    @ApiImplicitParam(name = "id",value = "管理员id",required = true,paramType = "query")
    public Result deleteInfo(@RequestParam(value = "id") Long []ids)
    {
        List<Long> deleteBatch = new ArrayList<>();
        for (Long addr:ids)
        {
            deleteBatch.add(addr);
        }
        int i = adminMapper.deleteBatchIds(deleteBatch);
        if (i==1)
        {
            return Result.response(ResultEnum.SUCCESS);
        }
        else
        {
            return Result.response(ResultEnum.ERROR);
        }
    }

}
